<?php

namespace app\appv110\model;

use think\Model;
use think\Db;

/*
 * 美容师
 */

class Staff extends Model {

    /*
     *给美容师添加考核满勤分
     */
    public static function addStaffScore($staff_id){
        //查美容师所在的团队配置的满勤天数是多少
        $team_id = Db::name('staff_team_details')->where('staff_id',$staff_id)->where('status','in','1,2')->value('team_id');
        if($team_id){
            $base_days = Db::name('staff_team')->where('id',$team_id)->value('base_days');
            //查美容师本月打卡次数
            $BeginDate = date('Y-m-01', strtotime(date("Y-m-d")));
            $endDate = date('Y-m-d', strtotime("$BeginDate +1 month"));
            $shiji_days = Db::name('staff_sign')->where('staff_id',$staff_id)
                    ->where('add_time','between',[$BeginDate,$endDate])
                    //->group("DATE_FORMAT(FROM_UNIXTIME(add_time),'%m-%d-%Y')")
                    ->group("date_format(add_time,'%m-%d-%Y')")
                    ->count();
            if($shiji_days >= $base_days){//打卡 达到满勤天数
                $row = Db::name('staff_assess_score')
                        ->where('staff_id',$staff_id)
                        ->where('score_type',2)
                        ->where('add_time','between',[$BeginDate,$endDate])
                        ->find();
                if(empty($row)){//本月还没有记录满勤分呢
                    $data['staff_id'] = $staff_id;
                    $data['type'] = 1;
                    $data['score_type'] = 2;
                    $data['score'] = 20;
                    $data['remarks'] = '当月出勤满'.$base_days.'天，基础分加20分';    
                    $data['add_time'] = date("Y-m-d 00::00:00",strtotime("+1 day"));
                    Db::name('staff_assess_score')->insert($data);
                }
            }
        }
    }

}
